#include <avr/io.h>
#include <avr/pgmspace.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include "main.h"
#include "pcd8544.h"

uint16_t temp;	//variable to hold temperature value

int main()
{
	DDRC = 0x00;	//analog pins set as input
	ADMUX = _BV(REFS0); //Vref=Vavcc, right justified, ADC0 single-end select
	ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); //start ADC with 128bit prescale for 125kHz

	LcdInit();	//initialize lcd
	LcdContrast(0x3F);	//set contrast
	_delay_ms(500);
	LcdClear();	//clear the lcd
	uint16_t Tens = temp%10;	//tens digit of temperature value
	uint16_t Ones = temp%100;	//ones digit of temperature value
	uint16_t Tenth = temp%1000;	//tenth digit of temperature value
	while (1)
	{
		ADCSRA |= (1<<ADSC);	//start conversion
		while((ADCSRA&(1<<ADIF))==0);	//wait for conversion
		temp = ADC;	//temp variable receives digital conversion value
		PORTD = ADCL;	//output to portd for error checking
		Tens = temp%10;	//tens value storage
		Ones = temp%100;	//ones value storage
		Tenth = temp%1000;	//tenth value storage
		byte value[4];	//declare char array for output
		sprintf(value,"%d%d.%d", Tens, Ones, Tenth);	//write to the array
		LcdClear();	//clear lcd
		LcdGotoXYFont(1,1);	//initiate position
		LcdFStr(FONT_1X,value);	//print temp
		LcdFStr(FONT_1X,(unsigned char*)PSTR(" F"));	//output formatting
		LcdUpdate();	//display data
		_delay_ms(3000);
	}
	return 0;
}